<template>
  <div>
    {{
      Condition.start(1)
        .eq('name', '张三')
        .caseViewTypeIsMonth(cdt => cdt.eq('age', 12))
        .end()
    }}
  </div>
  <br />
  <div>
    {{
      Condition.start(2)
        .eq('name', '张三')
        .caseViewTypeIsMonth(cdt => cdt.eq('age', 12))
        .end()
    }}
  </div>
  <br />
  <div>
    {{ Condition.start(2).eq('name', '张三').caseLastSevenDay('hello').end() }}
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

type LogicType = 'and' | 'or'
interface SingleConditon {
  field: string
  value: string | number | null
  logic: LogicType
  operate: 'eq' | 'ge' | 'le' | 'in'
}
type ConditionCaseCallbackMethod = (cdt: Condition) => void
class Condition {
  viewType = 1
  condition: SingleConditon[] = []
  constructor(viewType: number) {
    this.viewType = viewType
  }
  static start(viewType: number) {
    return new Condition(viewType)
  }
  end() {
    return this.condition
  }
  eq(field: string, value: string | number, logic: LogicType = 'and') {
    this.condition.push({
      field,
      value,
      logic,
      operate: 'eq',
    })
    return this
  }
  /**
   * 分支条件：如果是月视图，则执行call回调函数
   */
  caseViewTypeIsMonth(call: ConditionCaseCallbackMethod) {
    if (this.viewType === 2) {
      call(this)
    }
    return this
  }
  /**
   * 分支条件：如果是最近7天，则添加如果条件
   */
  caseLastSevenDay(field: string) {
    if (this.viewType === 2) {
      this.eq(field, 12)
      this.eq(field, 13)
    }
    return this
  }
}
</script>

<style scoped></style>
